package com.ghostworks.galileo.watcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;


public class Watcher {
	Twitter twitter = null;
	private String strUserID;
	
	// Make the default constructor private
	private Watcher() {
	}
	
	public Watcher(String strScreenName, String strPassword) {
		this.strUserID = strScreenName;
		twitter = new TwitterFactory().getInstance(strScreenName, strPassword);
	}
	
	public void InitateWatch() {
		List<Status> statuses = null;
		try {
			// Initiate DataObject
			WatcherData data = new WatcherData();
			Long lastTweet =data.getLastTweet(strUserID); 
			
			System.out.println("lastTweet::"+lastTweet);
			// Get the last 20 messages from the current timeline
			if (lastTweet == 0) {
				statuses = twitter.getFriendsTimeline();				
			} else {
				//Get the last tweet
				Paging paging = new Paging(lastTweet);
				statuses = twitter.getFriendsTimeline(paging);
			}

			for (Status status: statuses) {
				System.out.println(status.getText() + "::" + status.getUser().getScreenName() + "::" + status.getId());
			}
			
			if (statuses.size() != 0) {
				data.insertTags(locateTags(statuses), strUserID);
				data.updateLastTweet(strUserID, ((Status) statuses.get(0)).getId());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
	
	private ArrayList<Tweet> locateTags(List<Status> statuses) {
		String strTweet = "";
		String strTag = "";
		Tweet tweet = null;
		int intIndex = -1;
		/* HashMap to store tags linked at a TweetID level
		 * Here is an example
		 * TweetID 123456 has tags #Tag1 and #Tag2
		 * Therefore the HashMap will hold an ArrayList of tags 1 and 2 keyed with TweetID 
		 */
		HashMap<Long, ArrayList<String>> hmTweet = 
			new HashMap<Long, ArrayList<String>>();
		ArrayList<Tweet> lstTweets = new ArrayList<Tweet>();

		ArrayList<String> lstTags = null;
		for (Status status: statuses) {
			strTweet = status.getText();
			lstTags = new ArrayList<String>();
		
			// Locate the # character in the current string
			intIndex = strTweet.indexOf(Constants.HASH);
			
			// This loop exists when no more indices are found for #
			while(intIndex > -1) {
				// Get the tag as the characters between the hash and the first space.
				if (strTweet.indexOf(Constants.SPACE, intIndex) > -1) {
					strTag =  strTweet.substring(intIndex, 
							strTweet.indexOf(Constants.SPACE, intIndex + 1)) ;
				} else {
					strTag =  strTweet.substring(intIndex, strTweet.length()); 
				}
				System.out.println(strTag);
				System.out.println("strTweet" + strTweet);
				lstTags.add(strTag);
				// Get the next index.
				intIndex = strTweet.indexOf(Constants.HASH, intIndex+1);
			}

			// Since all the tags have been found add lstTags to hmTweet.
			if (lstTags.size() != 0) { 
				tweet = new Tweet();
				tweet.setLongTweetID(status.getId());
				tweet.setStrFriendID(status.getUser().getScreenName());
				tweet.setStrTags(lstTags);
				lstTweets.add(tweet);
			}
		}
		return lstTweets;
	}
	
}
